//
//  GWShopDetailViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/12.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWShopDetailViewController.h"
#import "GWHotItemsModel.h"
#import "GWShopDetailModel.h"
#import "GWShopHeaderView.h"
#import "GWShopDetailCell.h"


@interface GWShopDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@property (nonatomic, strong) UIView *redLine;

@property (nonatomic, strong) GWShopDetailModel *shopModel;

@end

@implementation GWShopDetailViewController
- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray ) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = @"品牌主页";
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = SCREEN_HEIGHT-100;
    [_mainTableView registerClass:[GWShopDetailCell class] forCellReuseIdentifier:@"collectionsCell"];
//    [_mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"webViewCell"];
    [self.view addSubview:_mainTableView];
    [self requestData];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    view.backgroundColor = [UIColor whiteColor];
    UIButton *itemButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [itemButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [itemButton setTitle:@"全部礼物" forState:(UIControlStateNormal)];
    itemButton.frame = CGRectMake(0, 0, SCREEN_WIDTH/2, 30);
    [itemButton addTarget:self action:@selector(handleItemButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    UIButton *brandButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [brandButton setTitle:@"品牌介绍" forState:(UIControlStateNormal)];
    [brandButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    brandButton.frame = CGRectMake(SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, 30);
    [brandButton addTarget:self action:@selector(hanleBrandButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    _redLine = [[UIView alloc] initWithFrame:CGRectMake(0, 28, SCREEN_WIDTH/2, 2)];
    _redLine.backgroundColor = [UIColor redColor];
    [view addSubview:itemButton];
    [view addSubview:brandButton];
    [view addSubview:_redLine];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (void)requestData {
    
    NSString *urlStr1 = [@"http://api.liwushuo.com/v2/brands/" stringByAppendingFormat:@"%@", self.id_s];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr1]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        GWShopDetailModel *model = [[GWShopDetailModel alloc] init];
        [model setValuesForKeysWithDictionary:dict[@"data"]];
        self.mainTableView.tableHeaderView = ({
            GWShopHeaderView *view = [[GWShopHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
            [view setConfigureModel:model];
            view;
        });
        _shopModel = model;
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
    
    NSString *urlStr2 = [@"http://api.liwushuo.com/v2/brands/" stringByAppendingFormat:@"%@%@", self.id_s, @"/items?limit=20&offset=0"];
    AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr2]]];
    [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        [self.dataSourceArray removeAllObjects];
        for (NSDictionary *itemDic in dict[@"data"][@"items"]) {
            GWHotItemsModel *model = [[GWHotItemsModel alloc] init];
            [model setValuesForKeysWithDictionary:itemDic];
            [self.dataSourceArray addObject:model];
        }
        [self.mainTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation1 start];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GWShopDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionsCell" forIndexPath:indexPath];
    cell.shopModel = self.shopModel;
    cell.dataSourceArray = self.dataSourceArray;
    cell.naviC = self.navigationController;
    return cell;
}

- (void)handleItemButtonAction {
    [UIView animateWithDuration:0.5 animations:^{
        _redLine.frame = CGRectMake(0, 28, SCREEN_WIDTH/2, 2);
    }];
    NSArray *array = [_mainTableView visibleCells];
    GWShopDetailCell *cell = (GWShopDetailCell *)array[0];
    [cell.contentView bringSubviewToFront:cell.collectionView];
    
}

- (void)hanleBrandButtonAction {
    [UIView animateWithDuration:0.5 animations:^{
        _redLine.frame = CGRectMake(SCREEN_WIDTH/2, 28, SCREEN_WIDTH/2, 2);
    }];
    NSArray *array = [_mainTableView visibleCells];
    GWShopDetailCell *cell = (GWShopDetailCell *)array[0];
    [cell.contentView bringSubviewToFront:cell.webView];
}

- (void)handleReturnButtonAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
